package com.ideal.frame.sys.controller;

import com.ideal.frame.common.anno.PermissionAnnotation;
import com.ideal.frame.common.dto.ResponseModel;
import com.ideal.frame.common.enumerate.SecurityPermission;
import com.ideal.frame.sys.entity.SysDict;
import com.ideal.frame.sys.form.search.DictSearchForm;
import com.ideal.frame.sys.service.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sys/dict")
@Api(tags = "系统字典")
public class SysDictController {

    @Autowired
    private SysDictService sysDictService;

    @ApiOperation(value = "系统字典(k/v)", httpMethod = "POST", produces = "application/json", response = ResponseModel.class)
    @PostMapping("kv")
    @PermissionAnnotation(SecurityPermission.sys_dict_view)
    public ResponseModel kv() {
        return ResponseModel.success(sysDictService.getSysDict());
    }

    @ApiOperation(value = "根据字典id获取字典信息", httpMethod = "GET", produces = "application/json", response = ResponseModel.class)
    @GetMapping("getById/{id}")
    @PermissionAnnotation(SecurityPermission.sys_dict_view)
    public ResponseModel getById(@PathVariable Long id) {
        return ResponseModel.success(sysDictService.getOne(id));
    }

    @ApiOperation(value = "根据字典id获取子级字典信息", httpMethod = "GET", produces = "application/json", response = ResponseModel.class)
    @GetMapping("getChildById/{id}")
    @PermissionAnnotation(SecurityPermission.sys_dict_view)
    public ResponseModel getChildById(@PathVariable Long id) {
        return ResponseModel.success(sysDictService.getChildById(id));
    }

    @ApiOperation(value = "根据字典编码获取子级字典信息", httpMethod = "GET", produces = "application/json", response = ResponseModel.class)
    @GetMapping("getChildByCode/{code}")
    public ResponseModel getChildByCode(@PathVariable String code) {
        return ResponseModel.success(sysDictService.getChildByCode(code));
    }

    @ApiOperation(value = "字典列表", httpMethod = "POST", produces = "application/json", response = ResponseModel.class)
    @PostMapping("list")
    @PermissionAnnotation(SecurityPermission.sys_dict_view)
    public ResponseModel list(@RequestBody DictSearchForm form) {
        return ResponseModel.success(sysDictService.getPage(form));
    }

    @ApiOperation(value = "字典新增/修改", httpMethod = "POST", produces = "application/json", response = ResponseModel.class)
    @PostMapping("save")
    @PermissionAnnotation(SecurityPermission.sys_dict_edit)
    public ResponseModel save(@RequestBody @Validated SysDict sysDict) {
        return ResponseModel.success(sysDictService.save(sysDict));
    }

    @ApiOperation(value = "字典删除", httpMethod = "DELETE", produces = "application/json", response = ResponseModel.class)
    @DeleteMapping("del")
    @PermissionAnnotation(SecurityPermission.sys_dict_del)
    public ResponseModel del(@RequestParam("ids") List<Long> ids) {
        sysDictService.del(ids);
        return ResponseModel.success();
    }
}
